package gui;

import generators.MenuGenerator;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.Dimension;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.JButton;

import common.HelperMethods;
import common.Settings;

import types.MenuBar;
import writers.MenuesWriter;

import java.awt.Rectangle;
import java.io.File;

public class FrmMain extends GeneralForm
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton btnMenues = null;
	private JButton btnGenerate = null;
	
	
	private MenuBar bar = null; 

	/**
	 * @param owner
	 */
	public FrmMain(/*Frame owner*/)
	{
		super(/*owner*/);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(321, 206);
		this.setTitle("Quick Generator");
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getBtnMenues(), null);
			jContentPane.add(getBtnGenerate(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes btnMenues	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnMenues()
	{
		if (btnMenues == null)
		{
			btnMenues = new JButton();
			btnMenues.setBounds(new Rectangle(21, 26, 79, 26));
			btnMenues.setText("Menues");
			btnMenues.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					openMenuesForm();
				}
			});
		}
		return btnMenues;
	}

	protected void openMenuesForm()
	{
		FrmMenuesMain f = new FrmMenuesMain();
		f.registerInterest(this);
		f.setVisible(true);
		
	}

	/**
	 * This method initializes btnGenerate	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnGenerate()
	{
		if (btnGenerate == null)
		{
			btnGenerate = new JButton();
			btnGenerate.setBounds(new Rectangle(80, 112, 118, 26));
			btnGenerate.setEnabled(false);
			btnGenerate.setText("Generate Code");
			btnGenerate.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					
					HelperMethods.deleteDir(new File(Settings.MenuesOutputDirectory));
					
					boolean success = (new File(Settings.MenuesOutputDirectory)).mkdirs();
				    if (!success) {
				        JOptionPane.showMessageDialog(null, "Error creating output directory.");
				    }
				    
				    if(bar!=null)
				    {
				    	MenuGenerator g = new MenuGenerator();
				    	g.generate(bar);
				    	MenuesWriter w = new MenuesWriter();
				    	w.write(bar);
				    }
				}
			});
		}
		return btnGenerate;
	}
	
	public void takeResult(Object result, String type)
	{
		if("MenuBar".equals(type))
		{
			bar  = (MenuBar)result;
		}
		
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
